package com.nq.service;

import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.nq.dao.MyDao;
import com.nq.utils.InvestingUtil;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadPoolExecutor;

@Service
public class TimerService implements InitializingBean {
    @Resource
    private MyDao myDao;

    public final static Map<Integer, String> countrys = new HashMap<>();

    static {
        // 新加坡-36 印度-14 日本-35 马来西亚-42 泰国-41 VN-178 台湾-46
        countrys.put(36, "sg");
        countrys.put(14, "in");
        countrys.put(35, "jp");
        countrys.put(42, "my");
        countrys.put(41, "th");
        countrys.put(178, "vn");
        countrys.put(46, "tw");
    }

    @Resource
    private JdbcTemplate jdbcTemplate;

//    @Scheduled(cron = "0 0/20 9-15 * * ?")
//    @Scheduled(cron = "0/30 9-21 * * * ?")
    public void updatePriceFromInv() {
        for (Integer id : countrys.keySet()) {
            updatePriceFromInv(id);
        }
    }

    public void updatePriceFromInv(int id) {
        try {
            int page = 0;
            while (true) {
                JSONArray stocks = InvestingUtil.getStocks(id, page);
                updatePriceFromInv(stocks);
                //initMarket(stocks);
                if (stocks.size() < 100) {
                    break;
                }
                page += 1;
                ThreadUtil.sleep(1500);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void updatePriceFromInv(JSONArray stocks) {
        new Thread(() -> {
            List<Map<String, Object>> list = new LinkedList<>();
            for (JSONObject stock : stocks.jsonIter()) {
                String symbol = stock.getStr("Symbol");
                String flag = stock.getStr("Flag");
                String stock_code = flag.toLowerCase() + "_" + symbol;
                Map<String, Object> map = new HashMap<>();
                map.put("stock_code", stock_code);
                map.put("open", stock.getBigDecimal("Open"));
                map.put("high", stock.getBigDecimal("High"));
                map.put("low", stock.getBigDecimal("Low"));
                map.put("close", stock.getBigDecimal("Last"));
                map.put("chg", stock.getBigDecimal("Chg"));
                map.put("chg_pct", stock.getBigDecimal("ChgPct"));
                map.put("vol", stock.getBigDecimal("Volume"));
                list.add(map);
            }
            MyDao.batchInsert("stock_real", list);
            System.out.println(list.size());
        }).start();
    }

    public int initMarket(int id) {
        int page = 0;
        try {
            while (true) {
                JSONArray stocks = InvestingUtil.getStocks(id, page);
                initMarket(stocks);
                if (stocks.size() < 100) {
                    break;
                }
                page += 1;
                ThreadUtil.sleep(1500);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return page;
    }

    private void initMarket(JSONArray stocks) {
        List<Map<String, Object>> list = new LinkedList<>();
        for (JSONObject stock : stocks.jsonIter()) {
            String symbol = stock.getStr("Symbol");
            String flag = stock.getStr("Flag");
            String stock_code = flag.toLowerCase() + "_" + symbol;
            Map<String, Object> map = new HashMap<>();
            map.put("stock_code", stock_code);
            map.put("stock_name", stock.getStr("Name"));
            map.put("stock_type", flag.toLowerCase());
            map.put("stock_gid", stock_code);
            map.put("investing_id", stock.getLong("Id"));
            list.add(map);
        }
        MyDao.batchInsert("stock", list);
        System.out.println(list.size());
    }


    @Override
    public void afterPropertiesSet() {

        new Thread(() -> {
            initMarket(46);
        }).start();
    }
}
